package qsoft.omanga.base.sao;

import android.content.Context;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import qsoft.omanga.business.entities.Manga;
import qsoft.omanga.common.utils.FileUtil;
import qsoft.omanga.common.utils.ServiceConfig;

/**
 * 
 * @author sontt
 * 
 */
public class MangaHandler extends DefaultHandler {

    /**
     * 
     */
    private Context context;
    /**
     * 
     */
    private Manga manga;
    /**
     * 
     */
    private List<Manga> listMangas = new ArrayList<Manga>();
    /**
     * 
     */
    private boolean mangaContent, mangaId, mangaName, mangaSubName,
            mangaCreator1, mangaCreator2, mangaDown, mangaGenre, mangaAdult,
            mangaIntro, mangaPicLarge, mangaPicSmall;
    /**
     * 
     */
    private String url, pathFileCache, imgName, fileName;
    /**
     * 
     */
    private String[] aURL = {
            "http://img.news.zing.vn/img/432/t432248.jpg",
            "http://img2.news.zing.vn/2012/05/25/5-7.jpg",
            "http://nl3.upanh.com/b1.s29.d4/a8f45cf18b5856a4143d70433cc5b32e_43859973.53281531278102001731412245437327.jpg",
            "http://nl1.upanh.com/b1.s28.d3/08c954b82473c332f72dfeda2d379b42_44335081.30338310150752393463507559158506.jpg",
            "http://nk0.upanh.com/b3.s27.d2/f097d7fe7f4df86f4867a925b34b7ed5_43391900.30623631194550554592410000191431.jpg",
            "http://nl2.upanh.com/b3.s29.d4/015edb71d593182c800acfb843bbaa4e_44380972.58099329262288748097110000199785.jpg",
            "http://nc9.upanh.com/b3.s16.d2/5436800c9edf95b26b9a98fe15f10b61_35483229.image14.jpg",
            "http://nj2.upanh.com/b2.s24.d3/0b49d096db7c7ed2af10ab5b5fd1f789_41852832.42083428630627051011515738560322.jpg",
            "http://img2.news.zing.vn/2012/05/25/11-5.jpg",
            "http://nm8.upanh.com/b1.s29.d4/8ac490e03e54cf3cc30b80427fed6abb_44802168.dd.jpg",
            "http://nl3.upanh.com/b2.s26.d1/c578f99c037958f0846f60af25b45f6a_44335233.42806410150613068413507559158506.jpg",
            "http://nl8.upanh.com/b3.s29.d4/7b9de8da44575f0e951b21f0cb92e1e4_44381188.15451729556628385329810000199785.jpg",
            "http://img2.news.zing.vn/2012/05/25/10-5.jpg",
            "http://nj8.upanh.com/b3.s15.d1/15a8ffa261b914f0c03960b66dabc5c8_41613818.2680962398078360317701682105n.jpg",
            "http://nm1.upanh.com/b2.s29.d2/8147bc4136fa1507a96982e79d99b7f4_44802591.gggggggggggggg.jpg",
            "http://img2.news.zing.vn/2012/02/14/17.jpg",
            "http://nl0.upanh.com/b1.s29.d3/39508d211374f75574183b319349a0ed_44335340.56428310150669476348507559158506.jpg",
            "http://nl5.upanh.com/b3.s28.d3/0d56813a76b26e07bffe1a949ed50da4_44381055.53259528487358492256810000199785.jpg",
            "http://img.news.zing.vn/img/432/t432238.jpg",
            "http://img2.news.zing.vn/2012/05/03/img6302.jpg",
            "http://nl8.upanh.com/b3.s27.d1/174ea3135cf744d5a5c9ce426148946f_44335158.38557810150660028153507559158506.jpg",
            "http://nl2.upanh.com/b1.s29.d2/3d7a645944ef37944d6752edf1714745_44311052.52419831529562854424510000191431.jpg",
            "http://img2.news.zing.vn/2012/05/27/5511353567500143798591436951323520169063402004950954n-1.jpg",
            "http://nl1.upanh.com/b6.s26.d1/59f902b4e96afaa5d95aba11b9389631_44380941.30654329259511081708210000199785.jpg",
            "http://img2.news.zing.vn/2012/02/02/p7.jpg",
            "http://nl4.upanh.com/b3.s28.d2/de887affe99604efedde71156ef72fd4_44032414.31598910150369434133507559158506.jpg",
            "http://nk8.upanh.com/b1.s24.d3/e8ab3d42bfd8a2ac3c28154a0d9b5bd6_42760208.54148929843043689743110000191431.jpg",
            "http://nl9.upanh.com/b2.s29.d2/e1d29f05f9da98eb479ef0b80b885bd6_44335259.48023710150726327018507559158506.jpg",
            "http://nl0.upanh.com/b5.s26.d1/ff88c701510bb397a937c9402a3df539_44380950.56211229259582415034410000199785.jpg",
            "http://nl7.upanh.com/b5.s29.d1/2303d489e5fd408ad1347bda22690e01_44335157.38454410150450816813507559158506.jpg", };
    
    /**
     * 
     */
    private int count = 0;

    /**
     * @param ctx
     *            Context
     */
    public MangaHandler(Context ctx) {
        context = ctx;
    }

    /**
     * 
     * @return List<Manga>
     */
    public List<Manga> getListManga() {
        ServiceHelper serviceHelper = new ServiceHelper();
        serviceHelper.parseXMLSAX(ServiceConfig.URL_TEST, this);
        return listMangas;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
     */
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // super.characters(ch, start, length);
        if (mangaContent) {
            if (mangaId) {
                manga.setId(new String(ch, start, length));
                mangaId = false;
            }
            if (mangaName) {
                manga.setName(new String(ch, start, length));
                mangaName = false;
            }
            if (mangaSubName) {
                manga.setSubName(new String(ch, start, length));
                mangaSubName = false;
            }
            if (mangaCreator1) {
                manga.setCreator1(new String(ch, start, length));
                mangaCreator1 = false;
            }
            if (mangaCreator2) {
                manga.setCreator2(new String(ch, start, length));
                mangaCreator2 = false;
            }
            if (mangaDown) {
                manga.setDownload(new String(ch, start, length));
                mangaDown = false;
            }
            if (mangaGenre) {
                manga.setGenre(new String(ch, start, length));
                mangaGenre = false;
            }
            if (mangaAdult) {
                String adult = new String(ch, start, length);
                if (adult.equalsIgnoreCase("true")) {
                    manga.setAdult(true);
                } else {
                    manga.setAdult(false);
                }
                mangaAdult = false;
            }
            if (mangaIntro) {
                manga.setIntro(new String(ch, start, length));
                mangaIntro = false;
            }
            if (mangaPicLarge) {
                // url = new String(ch, start, length);
                url = aURL[count];
                pathFileCache = FileUtil.getPathCache(context);
                // imgName = FileUtil.getImageName(manga.getId(),
                // FileUtil.IMAGE_LARGE);
                // fileName = pathFileCache + File.separator + imgName;
                // downImg = new DownloadImage();
                // downImg.setFileName(fileName);
                // downImg.execute(url);
                manga.setPicLarge(url);
                count++;
                mangaPicLarge = false;
            }
            if (mangaPicSmall) {
                // url = new String(ch, start, length);
                pathFileCache = FileUtil.getPathCache(context);
                imgName = FileUtil.getImageName(manga.getId(),
                        FileUtil.IMAGE_SMALL);
                fileName = pathFileCache + File.separator + imgName;
                // downImg = new DownloadImage();
                // downImg.setFileName(fileName);
                // downImg.execute(url);
                manga.setPicSmall(fileName);
                mangaPicSmall = false;
            }
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
     * java.lang.String, java.lang.String)
     */
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        super.endElement(uri, localName, qName);
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_CONTENT)) {
            if (manga != null) {
                listMangas.add(manga);
            }
            mangaContent = false;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.xml.sax.helpers.DefaultHandler#startDocument()
     */
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.xml.sax.helpers.DefaultHandler#endDocument()
     */
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
     * java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_CONTENT)) {
            mangaContent = true;
            manga = new Manga();
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_ID)) {
            mangaId = true;
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_NAME)) {
            mangaName = true;
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_SUBNAME)) {
            mangaSubName = true;
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_CREATOR1)) {
            mangaCreator1 = true;
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_CREATOR2)) {
            mangaCreator2 = true;
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_DOWNLOAD)) {
            mangaDown = true;
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_GENRE)) {
            mangaGenre = true;
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_ADULT)) {
            mangaAdult = true;
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_INTRO)) {
            mangaIntro = true;
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_PIC_URL1)) {
            mangaPicLarge = true;
        }
        if (qName.equalsIgnoreCase(ServiceConfig.MANGA_SMALLPIC_URL1)) {
            mangaPicSmall = true;
        }
        // super.startElement(uri, localName, qName, attributes);
    }

}
